﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{

    public class ScorecardController : Controller
    {
        MatchInterface matchInterface = new MatchInterface();
        RoundInterface roundInterface = new RoundInterface();
        ScheduleInterface scheduleInterface = new ScheduleInterface();
        ScoreCardInterface scoreCardInterface = new ScoreCardInterface();

        //
        // GET: /Scorecard/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index(int? id)
        {
            return View(scoreCardInterface.GetRoundMatchesEditModel(id));
        }

        //
        // POST: /Scorecard/RoundDropdown
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public JsonResult RoundDropdown(int scheduleID)
        {
            List<RoundsDropDownList> roundsDropDownList = new List<RoundsDropDownList>();
            foreach (Round round in roundInterface.GetAllScheduleRounds(scheduleID))
            {
                roundsDropDownList.Add(new RoundsDropDownList
                {
                    RoundID = round.RoundID,
                    RoundDate = round.RoundDate.ToShortDateString()
                });
            }
            if (roundsDropDownList.Count == 0) roundsDropDownList.Add(new RoundsDropDownList { RoundID = 0, RoundDate = "No rounds exist for this schedule." });
            return Json(new { roundsDropDownList });
        }

        //
        // GET: /Scorecard/Matches/5

        [Authorize(Roles = "Administrator")]
        public PartialViewResult Matches(int roundID)
        {
            //var matches = new List<Match>();
            //matches = matchInterface.GetRoundMatches(roundID).ToList();
            //ViewData["Matches"] = matches;
            //ViewData.Model = matches;
            //return View("RoundMatches", matches);
            PartialViewResult matchView = new PartialViewResult();
            IEnumerable<MatchViewModel> matches = matchInterface.GetMatchViewModels(roundID);
            matchView.ViewData.Model = matches;
            matchView.ViewName = "_Matches";

            //if(matchInterface.GetRoundIndividualMatches(roundID).Count() > 0) 
            return matchView;
        }

        //
        // GET: /Scorecard/Scorecards/5

        [Authorize(Roles = "Administrator")]
        public PartialViewResult Scorecards(int roundID)
        {
            PartialViewResult scoreCardView = new PartialViewResult();
            List<ScoreCardEditModel> roundScoreCardsEditModels = new List<ScoreCardEditModel>();
            IEnumerable<PlayerMatch> matchPlayers;
            foreach (Match match in matchInterface.GetRoundMatches(roundID))
            {
                matchPlayers = matchInterface.GetMatchPlayerMatches(match.MatchID);
                if (matchPlayers.Count() > 0)
                    roundScoreCardsEditModels.Add(scoreCardInterface.GetScoreCardEditModel(match.Round.CourseID, match.Round.TeeBoxID, match, matchPlayers));
            }

            scoreCardView.ViewData.Model = roundScoreCardsEditModels;
            scoreCardView.ViewName = "_Scorecards";

            return scoreCardView;
        }

        //
        // GET: /Scorecard/GenerateScorecards/5

        [Authorize(Roles = "Administrator")]
        public ActionResult GenerateScorecards(int id)
        {
            List<PlayerMatch> playerMatches = new List<PlayerMatch>();
            TeamInterface teamInterface = new TeamInterface();
            PlayerInterface playerInterface = new PlayerInterface();
            IEnumerable<Match> matchList = matchInterface.GetRoundMatches(id);
            foreach (Match match in matchList)
            {
                List<int> APlayers = new List<int>(2);
                List<int> BPlayers = new List<int>(2);
                IEnumerable<Team> teamList = matchInterface.GetMatchTeams(match.MatchID);

                foreach (Team team in teamList)
                {
                    IEnumerable<Player> teamPlayers = teamInterface.GetTeamPlayers(team.TeamID);
                    if (teamPlayers.First().PlayerHandicaps.OrderBy(h => h.CreatedOn).Last().Handicap <= 
                        teamPlayers.Last().PlayerHandicaps.OrderBy(h => h.CreatedOn).Last().Handicap)
                    {
                        APlayers.Add(teamPlayers.First().PlayerID);
                        BPlayers.Add(teamPlayers.Last().PlayerID);
                    }
                    else
                    {
                        APlayers.Add(teamPlayers.Last().PlayerID);
                        BPlayers.Add(teamPlayers.First().PlayerID);
                    }
                }

                playerMatches.Add(new PlayerMatch
                {
                    PlayerID = APlayers[0],
                    OpponentID = APlayers[1],
                    HandicapID = playerInterface.GetPlayersCurrentHandicap(APlayers[0]).HandicapID,
                    MatchID = match.MatchID
                });

                playerMatches.Add(new PlayerMatch
                {
                    PlayerID = APlayers[1],
                    OpponentID = APlayers[0],
                    HandicapID = playerInterface.GetPlayersCurrentHandicap(APlayers[1]).HandicapID,
                    MatchID = match.MatchID
                });

                playerMatches.Add(new PlayerMatch
                {
                    PlayerID = BPlayers[0],
                    OpponentID = BPlayers[1],
                    HandicapID = playerInterface.GetPlayersCurrentHandicap(BPlayers[0]).HandicapID,
                    MatchID = match.MatchID
                });

                playerMatches.Add(new PlayerMatch
                {
                    PlayerID = BPlayers[1],
                    OpponentID = BPlayers[0],
                    HandicapID = playerInterface.GetPlayersCurrentHandicap(BPlayers[1]).HandicapID,
                    MatchID = match.MatchID
                });
            }

            foreach (PlayerMatch playerMatch in playerMatches)
                matchInterface.AddPlayerMatch(playerMatch);
            matchInterface.Save();
            return RedirectToAction("Index", new { id = id });
        }


    }
}
